import { defineStore } from "pinia";
import req from "../../api/modules/meta";
import { onBridgeReady } from "@/infrastructure/utils/meta";
import { Base64 } from "js-base64";
export default defineStore("meta", {
  state: () => {
    return {
      cityList: [], //城市列表
      blockList: [], //区块列表
      blockListSold: [], //已选区块
      guideList: [], //导览列表
      guideList2: [], //导览列表
      guideIndex: 0,
      guideIndex2: 0,
      GuideDetalis: {}, //导览详情
      UserBlockInfo: {}, //用户区块信息
      blockVisitor: [], //用户区块访客
      blockInfoDetail: {}, //用户区块扩展信息
      orderInfo: null, //订单信息
      code: {}, //支付验证码
      paymentInfo: {}, //预支付信息
      PaymentStatus: {
        password: "",
      },
      privacy: {}, //隐私协议
      user: {}, //用户协议
      purchase: {}, //购买指南
      Connect: {}, //聊天用户信息
      CodeUrl: "", //客服二维码
      BlockExample: {}, //区块展示数据
      isAdd: {}, //是否允许添加购物车
      pid: 0, //分享链接的用户id
      wxAppId: {},
      out_trade_no: "", //商户订单号
      blockchain: {},
      areainfo: {}, //分享的城市信息
      isBlock: false,
      commissionerId: null,
      is_officer: false, //是否有权投诉
      cityID: 216,
      isLoading: false,
      blockShowInfo: {},
    };
  },
  actions: {
    // 设置城市ID
    setCityId(id) {
      this.cityID = id;
    },
    // 请求城市列表
    async getCityListApi(name = "") {
      const { data } = await req.getCityListApi(name);
      console.log(data);
      console.log("Object.keys(res.data)[0]");
      this.cityList = data;
    },
    // 请求区块列表
    async getBlockListApi() {
      let cityId = uni.getStorageSync("cityID");

      const { data } = await req.getBlockListApi(cityId);

      this.blockList = [];
      // 将区块列表处理成二维数组
      const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
      data.list.forEach((item) => {
        item.flag = false;
      });
      data.sold.forEach((item) => {
        item.flag = false;
      });
      arr.forEach((item, index) => {
        let newArr = [];
        data.list.forEach((items) => {
          if (item === items.x) {
            newArr.push(items);
          }
        });
        this.blockList.push(newArr);
      });

      this.blockListSold = data.sold;
    },
    // 获取导览列表
    async getGuideListApi(type) {
      try {
        const { data } = await req.getGuideListApi(type);
        this.guideList = data.reverse();
        console.log(data, "1");
      } catch (err) {
        console.log("异常错误", err);
      }
    },
    // 获取导览列表2
    async getGuideList2Api(id) {
      try {
        const { data } = await req.getGuideList2Api(id);
        this.guideList2 = data;
        console.log(data, "2");
      } catch (err) {
        console.log("异常错误", err);
      }
    },
    // 获取导览详情
    async getGuideDetalisApi(id) {
      try {
        const { data } = await req.getGuideDetalisApi(id);
        if (id == 53) {
          // 区价格表
          data.content = data.content.replace(
            /8区价格表/g,
            '<span id="_path" style="color: blue;">8区价格表</span>'
          );
        }
        this.GuideDetalis = data;
      } catch (err) {
        console.log("异常错误", err);
      }
    },
    // 获取用户区块信息
    async getUserBlockInfoApi(block_id) {
      try {
        const { data } = await req.getUserBlockInfoApi(block_id);
        this.UserBlockInfo = data;
        console.log(data, "获取用户区块信息");
      } catch (err) {
        console.log("异常错误", err);
      }
    },
    // 获取用户区块访客
    async getUserBlockVisitorApi() {
      try {
        const { data } = await req.getUserBlockVisitorApi(
          this.UserBlockInfo.id
        );
        console.log(data, "访客信息");
        this.blockVisitor = data;
      } catch (err) {
        console.log("异常错误", err);
      }
    },
    // 获取用户扩展信息
    async getUserBlockUserInfoDetailApi(block_id) {
      try {
        const { data } = await req.getUserBlockUserInfoDetailApi(block_id);
        this.blockInfoDetail = data;
      } catch (err) {
        console.log("异常错误", err);
      }
    },
    // 添加购物车
    async addShopApi(id) {
      try {
        const { data } = await req.addShopApi(id);
        this.isAdd = data;
      } catch (err) {
        console.log("异常错误", err);
        throw err;
      }
    },
    // 清空购物车api/order/purchase
    async clearShopApi() {
      try {
        const { data } = await req.clearShopApi();
      } catch (err) {
        console.log(err);
      }
    },
    // 下单
    async playsorderAPi(blockIds) {
      try {
        const { data } = await req.playsorderAPi(JSON.stringify(blockIds));
        this.orderInfo = data.order_no;
        uni.setStorageSync("order_no", data.order_no);
      } catch (err) {
        console.log(err);
      }
    },
    // 获取验证码
    async getCodeApi(phone) {
      try {
        const data = await req.getCodeApi(Number(phone));
        return data;
      } catch (err) {
        console.log(err);
      }
    },
    // 获取支付信息
    async getpaymentApi(phone, code, party_user_code, pay) {
      try {
        const { data } = await req.getpaymentApi(
          uni.getStorageSync("order_no"),
          phone,
          code,
          party_user_code,
          pay
        );
        if (data) {
          // window.open(data?.expend?.pay_info)

          if (pay === 1) {
            window.location.href = data?.expend?.pay_info;
          } else {
            if (uni.getStorageSync("wxCode") === 0) return;
            this.wxAppId = JSON.parse(data.pay_url);
            onBridgeReady(this.wxAppId);
          }
        }
        console.log(data);
        this.paymentInfo = data;
      } catch (err) {
        console.log("异常错误", err);
        this.paymentInfo = null;
        console.log(this.paymentInfo);
      }
    },
    // 检测支付状态
    async getPaymentStatus() {
      this.PaymentStatus = {
        password: "",
      }; // 清空
      try {
        const data = await req.getPaymentStatus(uni.getStorageSync("order_no"));
        if (data.code === 200) {
          const encode = Base64.encode(data.data.password); // 编码
          uni.setStorageSync("block_Account", {
            block_no: data.data.block_no,
            password: encode,
          });
        }
        this.PaymentStatus = data;
        console.log(data.code, data);
      } catch (err) {
        console.log("异常错误", err);
      }
    },
    // 获取帮助信息
    async getHelpApi(key) {
      try {
        console.log(key);
        const { data } = await req.getHelpApi(key);
        //将数据存储至对应对象中
        switch (key) {
          case "privacy_agreement":
            this.privacy = data;
            break;
          case "user_agreement":
            this.user = data;
            break;
          case "purchase_guide":
            this.purchase = data;
            break;
        }
      } catch (err) {
        console.log("异常错误", err);
      }
    },
    // 记录访客
    async getRecordsVisitorApi(block_id) {
      try {
        const { data } = await req.getRecordsVisitorApi(block_id);
      } catch (err) {
        console.log("异常错误", err);
      }
    },
    // 判断是否连接聊天
    async getisConnectApi(receiver_id) {
      try {
        const { data } = await req.getisConnectApi(receiver_id);
        this.Connect = data;
        console.log(data, "Connect");
      } catch (err) {
        console.log("异常错误", err);
      }
    },
    // 取消订单
    async cancelOrdertApi() {
      try {
        const { data } = await req.cancelOrdertApi(
          uni.getStorageSync("order_no")
        );
      } catch (err) {
        console.log("异常错误", err);
      }
    },
    // 获取客服二维码
    async getCodeUrlApi() {
      try {
        const { data } = await req.getCodeUrlApi();
        this.CodeUrl = data.url;
      } catch (err) {
        console.log("异常错误", err);
      }
    },
    // 获取区块展示信息
    async getBlockExampleApi() {
      try {
        const { data } = await req.getBlockExampleApi();
        this.BlockExample = data;
      } catch (err) {
        console.log("异常错误", err);
      }
    },
    // 更新访客是否隐藏
    async setvisitorupdateApi(is_stealth) {
      try {
        const { data } = await req.setvisitorupdateApi(
          this.UserBlockInfo.block_id,
          is_stealth
        );
        console.log(data, "更新访客状态");
      } catch (err) {
        console.log("异常错误", err);
      }
    },
    // 获取蚂蚁链相关信息
    async getblockchainApi(block_id) {
      try {
        const { data } = await req.getblockchainApi(block_id);
        console.log(data, "获取蚂蚁链");
        this.blockchain = data;
      } catch (err) {
        console.log("异常错误", err);
      }
    },
    // 分享城市信息
    async getareainfoApi(block_id) {
      try {
        const { data } = await req.getareainfoApi(block_id);
        console.log(data, "获取分享城市信息");
        this.areainfo = data;
        uni.setStorageSync("cityName", data.name);
        uni.setStorageSync("cityID", data.area_id);
        this.getBlockListApi();
      } catch (err) {
        console.log("异常错误", err);
      }
    },
    // 通过分享增加用户N值
    async addharenApi(type) {
      try {
        console.log(this.areainfo.area_id);
        const { data } = await req.addharenApi(
          type,
          this.areainfo.account_id,
          this.areainfo.area_id
        );
        console.log(data, "通过分享增加用户N值");
      } catch (err) {
        console.log("异常错误", err);
      }
    },
    // 专员推广
    async getCommissioner(code) {
      try {
        const result = await req.getQrcode(code);
        if (result && result.data && result.data.id && result.code == 200) {
          this.commissionerId = result.data.id;
        }
      } catch (err) {}
    },
    // 验证身份
    async getofficer() {
      try {
        const { data } = await req.getofficer();
        console.log(data);
        this.is_officer = data.is_officer;
      } catch (err) {
        throw err;
      }
    },
    // 投诉
    async getpunish() {
      try {
        const { data } = await req.getpunish(this.UserBlockInfo.id);
        uni.showToast({
          icon: "none",
          title: "处罚成功",
        });
      } catch (err) {}
    },
    // 用户展示信息
    async getblock_show_infoApi() {
      try {
        const { data } = await req.getblock_show_infoApi(this.UserBlockInfo.block_id);
        console.log(data,'getblock_show_infoApi');
        this.blockShowInfo = data;
      } catch (err) {}
    },
  },
  persist: true,
});
